package com.lcp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lcp.common.Result;
import com.lcp.entity.HealthLecture;
import com.lcp.service.HealthLectureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/health-lectures")
public class HealthLectureController {

    @Autowired
    private HealthLectureService healthLectureService;

    @GetMapping("/{id}")
    public Result<HealthLecture> getById(@PathVariable Long id) {
        healthLectureService.incrementViewCount(id);
        return Result.success(healthLectureService.getById(id));
    }

    @GetMapping("/page")
    public Result<IPage<HealthLecture>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String keyword) {
        return Result.success(healthLectureService.page(pageNum, pageSize, status, keyword));
    }

    @PostMapping
    public Result<HealthLecture> create(@RequestBody HealthLecture lecture) {
        return Result.success(healthLectureService.create(lecture));
    }

    @PutMapping("/{id}")
    public Result<HealthLecture> update(@PathVariable Long id, @RequestBody HealthLecture lecture) {
        lecture.setId(id);
        return Result.success(healthLectureService.update(lecture));
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        healthLectureService.delete(id);
        return Result.success();
    }

    @PostMapping("/{id}/like")
    public Result<Void> like(@PathVariable Long id) {
        healthLectureService.incrementLikeCount(id);
        return Result.success();
    }

    @GetMapping("/upcoming")
    public Result<List<HealthLecture>> getUpcomingLectures(
            @RequestParam(defaultValue = "5") Integer limit) {
        return Result.success(healthLectureService.getUpcomingLectures(limit));
    }

    @GetMapping("/ongoing")
    public Result<List<HealthLecture>> getOngoingLectures() {
        return Result.success(healthLectureService.getOngoingLectures());
    }

    @GetMapping("/finished")
    public Result<List<HealthLecture>> getFinishedLectures(
            @RequestParam(defaultValue = "5") Integer limit) {
        return Result.success(healthLectureService.getFinishedLectures(limit));
    }

    @GetMapping("/statistics")
    public Result<Map<String, Object>> getLectureStatistics() {
        return Result.success(healthLectureService.getLectureStatistics());
    }
} 